package cn.fl.spl.utils;

import cmm.comm.facade.upload.constant.SysUlFilesRecConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.DateUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.cmm.cache.framework.utils.PropertiesCacheUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.Date;

/**
 * Function: 文件工具类
 * Created by Administrator on 2017/3/2.
 */
public class FileUtil {

	private static final Logger logger = LoggerFactory.getLogger(FileUtil.class);

    /**
     * 获取文件保存服务器的路径
     * @param filePath
     * @return
     * @throws BaseException
     */
    public static String getServerFilePath(String filePath)throws BaseException {
        String prifixPath = PropertiesCacheUtils.getProperty(SysUlFilesRecConstant.SYS_FILE_UPLOAD_ROOT_PATH);//根目录
        StringBuffer savePath = new StringBuffer(prifixPath);//最终保存路径
        if(!savePath.toString().endsWith(SysUlFilesRecConstant.SYS_FILE_PATH_SLASH)){//判断是否以"/"结尾,不是加入斜杠
            savePath.append(SysUlFilesRecConstant.SYS_FILE_PATH_SLASH);
        }
        savePath.append(PropertiesCacheUtils.getProperty(SysUlFilesRecConstant.UPLOAD_COSTOMER_PATH));//自定义代码-其中包括系统代码
        if(StringUtils.isNotEmpty(filePath)){//如果不等于空则加入
            if(filePath.startsWith(SysUlFilesRecConstant.SYS_FILE_PATH_SLASH)){//判断是否以"/"开头
                savePath.append(filePath.substring(1));
            }else{
                savePath.append(filePath);
            }

        }
        if(!savePath.toString().endsWith(SysUlFilesRecConstant.SYS_FILE_PATH_SLASH)){//判断是否以"/"结尾
            savePath.append(SysUlFilesRecConstant.SYS_FILE_PATH_SLASH);
        }
        String currentTime = DateUtils.formatDateToString(new Date(), DateUtils.YYYYMMDD);
        savePath.append(currentTime);
        savePath.append(SysUlFilesRecConstant.SYS_FILE_PATH_SLASH);
        //检查目录
        File pathFile = new File(savePath.toString());
        if (!pathFile.exists()) {
            //文件夹不存 创建文件
            boolean createPath = pathFile.mkdirs();
            if(!createPath){
                BaseLogUtils.error(logger,"getServerFilePath","服务器文件目录创建失败==>"+savePath);
                throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "服务器文件目录创建失败!");
            }
        }
        return savePath.toString();
    }

    /**
     * 获取文件保存服务器的路径
     * @param filePath
     * @return
     * @throws BaseException
     */
    public static String getSavePath(String filePath)throws BaseException {
        String prifixPath = PropertiesCacheUtils.getProperty(SysUlFilesRecConstant.SYS_FILE_UPLOAD_ROOT_PATH);//根目录
        StringBuffer savePath = new StringBuffer(prifixPath);//最终保存路径
        if(!savePath.toString().endsWith(SysUlFilesRecConstant.SYS_FILE_PATH_SLASH)){//判断是否以"/"结尾,不是加入斜杠
            savePath.append(SysUlFilesRecConstant.SYS_FILE_PATH_SLASH);
        }
        savePath.append(PropertiesCacheUtils.getProperty(SysUlFilesRecConstant.UPLOAD_COSTOMER_PATH));//自定义代码-其中包括系统代码
        if(StringUtils.isNotEmpty(filePath)){//如果不等于空则加入
            if(filePath.startsWith(SysUlFilesRecConstant.SYS_FILE_PATH_SLASH)){//判断是否以"/"开头
                savePath.append(filePath.substring(1));
            }else{
                savePath.append(filePath);
            }

        }
        return savePath.toString();
    }
}
